package com.sskj.lwc;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;

public class PostRequestAndWriteToCsv {

    public static void main(String[] args) {
        try {
            // 发送POST请求
            String postUrl = "http://183.192.66.184/gateway/data/llg-stat/statChaozhamen";
            String postData = ""; // 替换为实际的POST数据

            HttpURLConnection connection = (HttpURLConnection) new URL(postUrl).openConnection();
            connection.setRequestMethod("POST");
            connection.setDoOutput(true);

            try (OutputStream outputStream = connection.getOutputStream();
                 BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(outputStream))) {
                writer.write(postData);
            }

            // 读取返回的JSON数据
            try (InputStream inputStream = connection.getInputStream();
                 BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream))) {
                StringBuilder response = new StringBuilder();
                String line;
                while ((line = reader.readLine()) != null) {
                    response.append(line);
                }

                // 解析JSON数据
                ObjectMapper objectMapper = new ObjectMapper();
                String string = response.toString();
                JSONObject jsonObject = JSON.parseObject(string);
                JSONArray data = jsonObject.getJSONArray("data");
                JsonNode jsonNode = objectMapper.readTree(data.toJSONString());

                // 将JSON数据写入CSV文件 TODO:
                CsvWriter.statChaozhamen(jsonNode, "潮闸门(1).csv");
                System.out.println("成功将数据写入CSV文件！");
            }

            connection.disconnect();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    static class CsvWriter {

        /**
         * 专用岸段
         *
         * @param dataNode
         * @param filePath
         * @throws IOException
         */
        static void statQiye(JsonNode dataNode, String filePath) throws IOException {
            // 创建CSV文件和写入器
            try (BufferedWriter writer = new BufferedWriter(new FileWriter(filePath))) {
                // 假设JSON数据中的每个元素都是对象，按照特定的顺序写入CSV文件
                writer.write("field0001,field0002,field0003,field0004,field0005,field0006,field0008," +
                        "field0009,field0010,field0011,field0015,field0016,leftright,mileage1,xx,yy\n");

                for (JsonNode element : dataNode) {
                    writer.write(element.get("field0001").asText() + ","
                            + element.get("field0002").asText() + ","
                            + element.get("field0003").asText() + ","
                            + element.get("field0004").asText() + ","
                            + element.get("field0005").asText() + ","
                            + element.get("field0006").asText() + ","
                            + element.get("field0008").asText() + ","
                            + element.get("field0009").asText() + ","
                            + element.get("field0010").asText() + ","
                            + element.get("field0011").asText() + ","
                            + element.get("field0015").asText() + ","
                            + element.get("field0016").asText() + ","
                            + element.get("leftright").asText() + ","
                            + element.get("mileage1").asText() + ","
                            + element.get("xxyy").asText() + "\n");
                }
            }
        }

        /**
         * 堤防结构信息
         *
         * @param dataNode
         * @param filePath
         * @throws IOException
         */
        static void statFangxunqiang(JsonNode dataNode, String filePath) throws IOException {
            // 创建CSV文件和写入器
            try (BufferedWriter writer = new BufferedWriter(new FileWriter(filePath))) {
                // 写入CSV文件头部
                writer.write("field0001,field0002,field0003,field0004,field0005,field0006,field0007,field0008," +
                        "field0009,field0010,field0018,leftright,mileage1,xx,yy\n");

                // 假设JSON数据中的每个元素都是对象，按照特定的顺序写入CSV文件
                for (JsonNode element : dataNode) {
                    writeField(writer, element, "field0001");
                    writeField(writer, element, "field0002");
                    writeField(writer, element, "field0003");
                    writeField(writer, element, "field0004");
                    writeField(writer, element, "field0005");
                    writeField(writer, element, "field0006");
                    writeField(writer, element, "field0007");
                    writeField(writer, element, "field0008");
                    writeField(writer, element, "field0009");
                    writeField(writer, element, "field0010");
                    writeField(writer, element, "field0018");
                    writeField(writer, element, "leftright");
                    writeField(writer, element, "mileage1");
                    writeField(writer, element, "xxyy");

                    writer.write("\n");
                }
            }
        }

        /**
         * 支河桥梁
         *
         * @param dataNode
         * @param filePath
         * @throws IOException
         */
        static void statZhiheqiaoliang(JsonNode dataNode, String filePath) throws IOException {
            // 创建CSV文件和写入器
            try (BufferedWriter writer = new BufferedWriter(new FileWriter(filePath))) {
                // 写入CSV文件头部
                writer.write("field0001,field0002,field0003,field0004,field0005,field0006,field0007,field0008," +
                        "leftright,mileage1,xx,yy\n");

                // 假设JSON数据中的每个元素都是对象，按照特定的顺序写入CSV文件
                for (JsonNode element : dataNode) {
                    writeField(writer, element, "field0001");
                    writeField(writer, element, "field0002");
                    writeField(writer, element, "field0003");
                    writeField(writer, element, "field0004");
                    writeField(writer, element, "field0005");
                    writeField(writer, element, "field0006");
                    writeField(writer, element, "field0007");
                    writeField(writer, element, "field0008");
                    writeField(writer, element, "leftright");
                    writeField(writer, element, "mileage1");
                    writeField(writer, element, "xxyy");

                    writer.write("\n");
                }
            }
        }

        /**
         * 伸缩缝
         *
         * @param dataNode
         * @param filePath
         * @throws IOException
         */
        static void statShensuofeng(JsonNode dataNode, String filePath) throws IOException {
            // 创建CSV文件和写入器
            try (BufferedWriter writer = new BufferedWriter(new FileWriter(filePath))) {
                // 写入CSV文件头部
                writer.write("field0001,field0002,field0003,field0004,field0005,field0006,field0007,field0008," +
                        "field0009,field0015,field0016,leftright,mileage1,xx,yy\n");

                // 假设JSON数据中的每个元素都是对象，按照特定的顺序写入CSV文件
                for (JsonNode element : dataNode) {
                    writeField(writer, element, "field0001");
                    writeField(writer, element, "field0002");
                    writeField(writer, element, "field0003");
                    writeField(writer, element, "field0004");
                    writeField(writer, element, "field0005");
                    writeField(writer, element, "field0006");
                    writeField(writer, element, "field0007");
                    writeField(writer, element, "field0008");
                    writeField(writer, element, "field0009");
                    writeField(writer, element, "field0015");
                    writeField(writer, element, "field0016");
                    writeField(writer, element, "leftright");
                    writeField(writer, element, "mileage1");
                    writeField(writer, element, "xxyy");

                    writer.write("\n");
                }
            }
        }

        /**
         * 防汛通道
         *
         * @param dataNode
         * @param filePath
         * @throws IOException
         */
        static void statFangxuntongdao(JsonNode dataNode, String filePath) throws IOException {
            // 创建CSV文件和写入器
            try (BufferedWriter writer = new BufferedWriter(new FileWriter(filePath))) {
                // 写入CSV文件头部
                writer.write("field0001,field0002,field0003,field0004,field0006,field0007,field0008," +
                        "field0010,field0011,leftright,mileage1,xx,yy\n");

                // 假设JSON数据中的每个元素都是对象，按照特定的顺序写入CSV文件
                for (JsonNode element : dataNode) {
                    writeField(writer, element, "field0001");
                    writeField(writer, element, "field0002");
                    writeField(writer, element, "field0003");
                    writeField(writer, element, "field0004");
                    writeField(writer, element, "field0006");
                    writeField(writer, element, "field0007");
                    writeField(writer, element, "field0008");
                    writeField(writer, element, "field0010");
                    writeField(writer, element, "field0011");
                    writeField(writer, element, "leftright");
                    writeField(writer, element, "mileage1");
                    writeField(writer, element, "xxyy");

                    writer.write("\n");
                }
            }
        }

        /**
         * 潮拍门
         *
         * @param dataNode
         * @param filePath
         * @throws IOException
         */
        static void statChaopaimen(JsonNode dataNode, String filePath) throws IOException {
            // 创建CSV文件和写入器
            try (BufferedWriter writer = new BufferedWriter(new FileWriter(filePath))) {
                // 写入CSV文件头部
                writer.write("field0001,field0002,field0003,field0005,field0006,field0007," +
                        "field0009,field0015,field0016,leftright,mileage1,xx,yy\n");

                // 假设JSON数据中的每个元素都是对象，按照特定的顺序写入CSV文件
                for (JsonNode element : dataNode) {
                    writeField(writer, element, "field0001");
                    writeField(writer, element, "field0002");
                    writeField(writer, element, "field0003");
                    writeField(writer, element, "field0005");
                    writeField(writer, element, "field0006");
                    writeField(writer, element, "field0007");
                    writeField(writer, element, "field0009");
                    writeField(writer, element, "field0015");
                    writeField(writer, element, "field0016");
                    writeField(writer, element, "leftright");
                    writeField(writer, element, "mileage1");
                    writeField(writer, element, "xxyy");

                    writer.write("\n");
                }
            }
        }

        /**
         * 潮闸门
         *
         * @param dataNode
         * @param filePath
         * @throws IOException
         */
        static void statChaozhamen(JsonNode dataNode, String filePath) throws IOException {
            // 创建CSV文件和写入器
            try (BufferedWriter writer = new BufferedWriter(new FileWriter(filePath))) {
                // 写入CSV文件头部
                writer.write("field0001,field0002,field0003,field0004,field0005,field0006,field0007," +
                        "field0008,field0009,field0010,field0011,leftright,mileage1,xx,yy\n");

                // 假设JSON数据中的每个元素都是对象，按照特定的顺序写入CSV文件
                for (JsonNode element : dataNode) {
                    writeField(writer, element, "field0001");
                    writeField(writer, element, "field0002");
                    writeField(writer, element, "field0003");
                    writeField(writer, element, "field0004");
                    writeField(writer, element, "field0005");
                    writeField(writer, element, "field0006");
                    writeField(writer, element, "field0007");
                    writeField(writer, element, "field0008");
                    writeField(writer, element, "field0009");
                    writeField(writer, element, "field0010");
                    writeField(writer, element, "field0011");
                    writeField(writer, element, "leftright");
                    writeField(writer, element, "mileage1");
                    writeField(writer, element, "xxyy");

                    writer.write("\n");
                }
            }
        }

        /**
         * 水闸
         *
         * @param dataNode
         * @param filePath
         * @throws IOException
         */
        static void statShuizha(JsonNode dataNode, String filePath) throws IOException {
            // 创建CSV文件和写入器
            try (BufferedWriter writer = new BufferedWriter(new FileWriter(filePath))) {
                // 写入CSV文件头部
                writer.write("field0002,field0003,field0004,field0005,field0006,field0007," +
                        "field0013,field0014,field0015,leftright,mileage1,xx,yy\n");

                // 假设JSON数据中的每个元素都是对象，按照特定的顺序写入CSV文件
                for (JsonNode element : dataNode) {
                    writeField(writer, element, "field0002");
                    writeField(writer, element, "field0003");
                    writeField(writer, element, "field0004");
                    writeField(writer, element, "field0005");
                    writeField(writer, element, "field0006");
                    writeField(writer, element, "field0007");
                    writeField(writer, element, "field0013");
                    writeField(writer, element, "field0014");
                    writeField(writer, element, "field0015");
                    writeField(writer, element, "leftright");
                    writeField(writer, element, "mileage1");
                    writeField(writer, element, "xxyy");

                    writer.write("\n");
                }
            }
        }

        /**
         * 里程桩号
         *
         * @param dataNode
         * @param filePath
         * @throws IOException
         */
        static void statLichengzhuanghao(JsonNode dataNode, String filePath) throws IOException {
            // 创建CSV文件和写入器
            try (BufferedWriter writer = new BufferedWriter(new FileWriter(filePath))) {
                // 写入CSV文件头部
                writer.write("field0001,field0002,field0003,field0004,field0006,field0007," +
                        "field0010,field0015,field0016,leftright,mileage1,xx,yy\n");

                // 假设JSON数据中的每个元素都是对象，按照特定的顺序写入CSV文件
                for (JsonNode element : dataNode) {
                    writeField(writer, element, "field0001");
                    writeField(writer, element, "field0002");
                    writeField(writer, element, "field0003");
                    writeField(writer, element, "field0004");
                    writeField(writer, element, "field0006");
                    writeField(writer, element, "field0007");
                    writeField(writer, element, "field0010");
                    writeField(writer, element, "field0015");
                    writeField(writer, element, "field0016");
                    writeField(writer, element, "leftright");
                    writeField(writer, element, "mileage1");
                    writeField(writer, element, "xxyy");

                    writer.write("\n");
                }
            }
        }

        /**
         * 工作井
         *
         * @param dataNode
         * @param filePath
         * @throws IOException
         */
        static void statGongzuojing(JsonNode dataNode, String filePath) throws IOException {
            // 创建CSV文件和写入器
            try (BufferedWriter writer = new BufferedWriter(new FileWriter(filePath))) {
                // 写入CSV文件头部
                writer.write("field0001,field0002,field0003,field0004,field0005,field0006,field0007," +
                        "field0015,field0016,leftright,mileage1,xx,yy\n");
                // 假设JSON数据中的每个元素都是对象，按照特定的顺序写入CSV文件
                for (JsonNode element : dataNode) {
                    writeField(writer, element, "field0001");
                    writeField(writer, element, "field0002");
                    writeField(writer, element, "field0003");
                    writeField(writer, element, "field0004");
                    writeField(writer, element, "field0005");
                    writeField(writer, element, "field0006");
                    writeField(writer, element, "field0007");
                    writeField(writer, element, "field0015");
                    writeField(writer, element, "field0016");
                    writeField(writer, element, "leftright");
                    writeField(writer, element, "mileage1");
                    writeField(writer, element, "xxyy");
                    writer.write("\n");
                }
            }
        }

        /**
         * 界桩
         *
         * @param dataNode
         * @param filePath
         * @throws IOException
         */
        static void statJiezhuang(JsonNode dataNode, String filePath) throws IOException {
            // 创建CSV文件和写入器
            try (BufferedWriter writer = new BufferedWriter(new FileWriter(filePath))) {
                // 写入CSV文件头部
                writer.write("field0001,field0002,field0003,field0004,field0005,field0006," +
                        "leftright,mileage1,xx,yy\n");
                // 假设JSON数据中的每个元素都是对象，按照特定的顺序写入CSV文件
                for (JsonNode element : dataNode) {
                    writeField(writer, element, "field0001");
                    writeField(writer, element, "field0002");
                    writeField(writer, element, "field0003");
                    writeField(writer, element, "field0004");
                    writeField(writer, element, "field0005");
                    writeField(writer, element, "field0006");
                    writeField(writer, element, "leftright");
                    writeField(writer, element, "mileage1");
                    writeField(writer, element, "xxyy");
                    writer.write("\n");
                }
            }
        }

        /**
         * 限高
         *
         * @param dataNode
         * @param filePath
         * @throws IOException
         */
        static void statXiangao(JsonNode dataNode, String filePath) throws IOException {
            // 创建CSV文件和写入器
            try (BufferedWriter writer = new BufferedWriter(new FileWriter(filePath))) {
                // 写入CSV文件头部
                writer.write("field0001,field0002,field0003,field0004,field0007,field0008,field0009,field0010," +
                        "leftright,mileage1,xx,yy\n");
                // 假设JSON数据中的每个元素都是对象，按照特定的顺序写入CSV文件
                for (JsonNode element : dataNode) {
                    writeField(writer, element, "field0001");
                    writeField(writer, element, "field0002");
                    writeField(writer, element, "field0003");
                    writeField(writer, element, "field0004");
                    writeField(writer, element, "field0007");
                    writeField(writer, element, "field0008");
                    writeField(writer, element, "field0009");
                    writeField(writer, element, "field0010");
                    writeField(writer, element, "leftright");
                    writeField(writer, element, "mileage1");
                    writeField(writer, element, "xxyy");
                    writer.write("\n");
                }
            }
        }

        /**
         * 标志牌
         *
         * @param dataNode
         * @param filePath
         * @throws IOException
         */
        static void statBiaozhipai(JsonNode dataNode, String filePath) throws IOException {
            // 创建CSV文件和写入器
            try (BufferedWriter writer = new BufferedWriter(new FileWriter(filePath))) {
                // 写入CSV文件头部
                writer.write("field0001,field0002,field0003,field0004,field0005,field0006,field0007,field0008,field0009,field0010," +
                        "leftright,mileage1,xx,yy\n");
                // 假设JSON数据中的每个元素都是对象，按照特定的顺序写入CSV文件
                for (JsonNode element : dataNode) {
                    writeField(writer, element, "field0001");
                    writeField(writer, element, "field0002");
                    writeField(writer, element, "field0003");
                    writeField(writer, element, "field0004");
                    writeField(writer, element, "field0005");
                    writeField(writer, element, "field0006");
                    writeField(writer, element, "field0007");
                    writeField(writer, element, "field0008");
                    writeField(writer, element, "field0009");
                    writeField(writer, element, "field0010");
                    writeField(writer, element, "leftright");
                    writeField(writer, element, "mileage1");
                    writeField(writer, element, "xxyy");
                    writer.write("\n");
                }
            }
        }

        /**
         * 上堤道路
         *
         * @param dataNode
         * @param filePath
         * @throws IOException
         */
        static void statShangdidaolu(JsonNode dataNode, String filePath) throws IOException {
            // 创建CSV文件和写入器
            try (BufferedWriter writer = new BufferedWriter(new FileWriter(filePath))) {
                // 写入CSV文件头部
                writer.write("field0001,field0002,field0003,field0004,field0005,field0006,field0007,field0008,field0009,field0010," +
                        "leftright,mileage1,xx,yy\n");
                // 假设JSON数据中的每个元素都是对象，按照特定的顺序写入CSV文件
                for (JsonNode element : dataNode) {
                    writeField(writer, element, "field0001");
                    writeField(writer, element, "field0002");
                    writeField(writer, element, "field0003");
                    writeField(writer, element, "field0004");
                    writeField(writer, element, "field0005");
                    writeField(writer, element, "field0006");
                    writeField(writer, element, "field0007");
                    writeField(writer, element, "field0008");
                    writeField(writer, element, "field0009");
                    writeField(writer, element, "field0010");
                    writeField(writer, element, "leftright");
                    writeField(writer, element, "mileage1");
                    writeField(writer, element, "xxyy");
                    writer.write("\n");
                }
            }
        }

        /**
         * 监视器
         *
         * @param dataNode
         * @param filePath
         * @throws IOException
         */
        static void statJianshiqi(JsonNode dataNode, String filePath) throws IOException {
            // 创建CSV文件和写入器
            try (BufferedWriter writer = new BufferedWriter(new FileWriter(filePath))) {
                // 写入CSV文件头部
                writer.write("field0001,field0002,field0003,field0004,field0005,field0006,field0007,field0008,field0009,field0010," +
                        "leftright,mileage1,xx,yy\n");
                // 假设JSON数据中的每个元素都是对象，按照特定的顺序写入CSV文件
                for (JsonNode element : dataNode) {
                    writeField(writer, element, "field0001");
                    writeField(writer, element, "field0002");
                    writeField(writer, element, "field0003");
                    writeField(writer, element, "field0004");
                    writeField(writer, element, "field0005");
                    writeField(writer, element, "field0006");
                    writeField(writer, element, "field0007");
                    writeField(writer, element, "field0008");
                    writeField(writer, element, "field0009");
                    writeField(writer, element, "field0010");
                    writeField(writer, element, "leftright");
                    writeField(writer, element, "mileage1");
                    writeField(writer, element, "xxyy");
                    writer.write("\n");
                }
            }
        }

        /**
         * 墓地
         *
         * @param dataNode
         * @param filePath
         * @throws IOException
         */
        static void statMudi(JsonNode dataNode, String filePath) throws IOException {
            // 创建CSV文件和写入器
            try (BufferedWriter writer = new BufferedWriter(new FileWriter(filePath))) {
                // 写入CSV文件头部
                writer.write("field0001,field0002,field0003,field0004,field0005,field0006,field0007,field0008,field0009," +
                        "leftright,mileage1,xx,yy\n");
                // 假设JSON数据中的每个元素都是对象，按照特定的顺序写入CSV文件
                for (JsonNode element : dataNode) {
                    writeField(writer, element, "field0001");
                    writeField(writer, element, "field0002");
                    writeField(writer, element, "field0003");
                    writeField(writer, element, "field0004");
                    writeField(writer, element, "field0005");
                    writeField(writer, element, "field0006");
                    writeField(writer, element, "field0007");
                    writeField(writer, element, "field0008");
                    writeField(writer, element, "field0009");
                    writeField(writer, element, "leftright");
                    writeField(writer, element, "mileage1");
                    writeField(writer, element, "xxyy");
                    writer.write("\n");
                }
            }
        }

        /**
         * 电力及通讯设施
         *
         * @param dataNode
         * @param filePath
         * @throws IOException
         */
        static void statDengta(JsonNode dataNode, String filePath) throws IOException {
            // 创建CSV文件和写入器
            try (BufferedWriter writer = new BufferedWriter(new FileWriter(filePath))) {
                // 写入CSV文件头部
                writer.write("field0001,field0002,field0003,field0004,field0005,field0006,field0008," +
                        "field0011,leftright,mileage1,xx,yy\n");
                // 假设JSON数据中的每个元素都是对象，按照特定的顺序写入CSV文件
                for (JsonNode element : dataNode) {
                    writeField(writer, element, "field0001");
                    writeField(writer, element, "field0002");
                    writeField(writer, element, "field0003");
                    writeField(writer, element, "field0004");
                    writeField(writer, element, "field0005");
                    writeField(writer, element, "field0006");
                    writeField(writer, element, "field0008");
                    writeField(writer, element, "field0011");
                    writeField(writer, element, "leftright");
                    writeField(writer, element, "mileage1");
                    writeField(writer, element, "xxyy");
                    writer.write("\n");
                }
            }
        }

        /**
         * 管线
         *
         * @param dataNode
         * @param filePath
         * @throws IOException
         */
        static void statGuanxian(JsonNode dataNode, String filePath) throws IOException {
            // 创建CSV文件和写入器
            try (BufferedWriter writer = new BufferedWriter(new FileWriter(filePath))) {
                // 写入CSV文件头部
                writer.write("field0001,field0002,field0003,field0004,field0005,field0006,field0007,field0008," +
                        "leftright,mileage1,xx,yy\n");
                // 假设JSON数据中的每个元素都是对象，按照特定的顺序写入CSV文件
                for (JsonNode element : dataNode) {
                    writeField(writer, element, "field0001");
                    writeField(writer, element, "field0002");
                    writeField(writer, element, "field0003");
                    writeField(writer, element, "field0004");
                    writeField(writer, element, "field0005");
                    writeField(writer, element, "field0006");
                    writeField(writer, element, "field0007");
                    writeField(writer, element, "field0008");
                    writeField(writer, element, "leftright");
                    writeField(writer, element, "mileage1");
                    writeField(writer, element, "xxyy");
                    writer.write("\n");
                }
            }
        }

        /**
         * 跨干河桥梁
         *
         * @param dataNode
         * @param filePath
         * @throws IOException
         */
        static void statQiaoliang(JsonNode dataNode, String filePath) throws IOException {
            // 创建CSV文件和写入器
            try (BufferedWriter writer = new BufferedWriter(new FileWriter(filePath))) {
                // 写入CSV文件头部
                writer.write("field0001,field0002,field0003,field0004,field0005,field0006,field0007,field0014," +
                        "leftright,mileage1,xx,yy\n");
                // 假设JSON数据中的每个元素都是对象，按照特定的顺序写入CSV文件
                for (JsonNode element : dataNode) {
                    writeField(writer, element, "field0001");
                    writeField(writer, element, "field0002");
                    writeField(writer, element, "field0003");
                    writeField(writer, element, "field0004");
                    writeField(writer, element, "field0005");
                    writeField(writer, element, "field0006");
                    writeField(writer, element, "field0007");
                    writeField(writer, element, "field0014");
                    writeField(writer, element, "leftright");
                    writeField(writer, element, "mileage1");
                    writeField(writer, element, "xxyy");
                    writer.write("\n");
                }
            }
        }

        /**
         * 险公薄弱段
         *
         * @param dataNode
         * @param filePath
         * @throws IOException
         */
        static void statXiangongboruo(JsonNode dataNode, String filePath) throws IOException {
            // 创建CSV文件和写入器
            try (BufferedWriter writer = new BufferedWriter(new FileWriter(filePath))) {
                // 写入CSV文件头部
                writer.write("field0001,field0002,field0003,field0004,field0005,field0006,field0007," +
                        "leftright,mileage1,xx,yy\n");
                // 假设JSON数据中的每个元素都是对象，按照特定的顺序写入CSV文件
                for (JsonNode element : dataNode) {
                    writeField(writer, element, "field0001");
                    writeField(writer, element, "field0002");
                    writeField(writer, element, "field0003");
                    writeField(writer, element, "field0004");
                    writeField(writer, element, "field0005");
                    writeField(writer, element, "field0006");
                    writeField(writer, element, "field0007");
                    writeField(writer, element, "leftright");
                    writeField(writer, element, "mileage1");
                    writeField(writer, element, "xxyy");
                    writer.write("\n");
                }
            }
        }

        /**
         * 警示牌
         *
         * @param dataNode
         * @param filePath
         * @throws IOException
         */
        static void statJingshipai(JsonNode dataNode, String filePath) throws IOException {
            // 创建CSV文件和写入器
            try (BufferedWriter writer = new BufferedWriter(new FileWriter(filePath))) {
                // 写入CSV文件头部
                writer.write("field0001,field0002,field0003,field0004,field0005,field0006,field0008,field0009," +
                        "leftright,mileage1,xx,yy\n");
                // 假设JSON数据中的每个元素都是对象，按照特定的顺序写入CSV文件
                for (JsonNode element : dataNode) {
                    writeField(writer, element, "field0001");
                    writeField(writer, element, "field0002");
                    writeField(writer, element, "field0003");
                    writeField(writer, element, "field0004");
                    writeField(writer, element, "field0005");
                    writeField(writer, element, "field0006");
                    writeField(writer, element, "field0008");
                    writeField(writer, element, "field0009");
                    writeField(writer, element, "leftright");
                    writeField(writer, element, "mileage1");
                    writeField(writer, element, "xxyy");
                    writer.write("\n");
                }
            }
        }




        private static void writeField(BufferedWriter writer, JsonNode element, String fieldName) throws IOException {
            if (fieldName.equals("xxyy")){
                JsonNode fieldNode = element.get(fieldName);
                if (fieldNode == null) {
                    writer.write(",,");
                }else {
                    writer.write((fieldNode != null) ? fieldNode.asText() : "");
                    writer.write(",");

                }
            }else {
                // 检查字段是否存在，如果存在则写入，否则写入空字符串
                JsonNode fieldNode = element.get(fieldName);
                writer.write((fieldNode != null) ? fieldNode.asText() : "");
                writer.write(",");
            }
        }
    }


}
